package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.back.domain.MemberNotice
import com.ruoyi.back.domain.UserMemberNoticeMessage
import com.ruoyi.back.enums.NoticeTypeEnum
import com.ruoyi.back.enums.StatusTypeEnum
import com.ruoyi.back.mapper.UserMemberMessageMapper
import com.ruoyi.back.service.MemberNoticeService
import com.ruoyi.back.service.UserMemberMessageService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class UserMemberMessageServiceImpl : ServiceImpl<UserMemberMessageMapper, UserMemberNoticeMessage>(),
    UserMemberMessageService {

    @Autowired
    private lateinit var memberNoticeService: MemberNoticeService


    override fun sendUserMessage(noticeTypeEnum: NoticeTypeEnum, userId: Long) {
        memberNoticeService.getOne(
            KtQueryWrapper(MemberNotice::class.java)
                .eq(MemberNotice::noticeType, noticeTypeEnum.code)
                .eq(MemberNotice::status, StatusTypeEnum.OPEN.code)
                .orderByDesc(MemberNotice::createTime)
                .last("limit 1")
        )
            ?.let {
                save(UserMemberNoticeMessage().apply {
                    noticeType = noticeTypeEnum.code
                    noticeContent = it.noticeContent
                    createBy = userId
                })
                // TODO: websocket通知
            }
    }

}